package org.example.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Timer;

/**
 * 数据库默认填充
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Value("${user.defaultAvatar}")
    private String defaultAvatar;

    @Override
    public void insertFill(MetaObject metaObject) {
        if(metaObject.hasSetter("plus")){
            this.strictInsertFill(metaObject, "plus", Integer.class,0);
        }
        if(metaObject.hasSetter("icon")){
            String path = File.separator  + "images"+ File.separator  + "avatar" + File.separator + defaultAvatar;
            this.strictInsertFill(metaObject, "icon", String.class,path);
        }
        if(metaObject.hasSetter("rate")){
            this.strictInsertFill(metaObject, "rate", Integer.class,0);
        }
        if(metaObject.hasSetter("createTime")){
            this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        }
        if(metaObject.hasSetter("payTime")){
            this.strictInsertFill(metaObject, "payTime", Date.class, new Date());
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if(metaObject.hasSetter("updateTime")){
            this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        }
    }
}
